<?php
/**
 * PsiFramework
 *
 * Php Simple Framework
 *
 * PHP version 5.3
 *
 * @category  PsiFramework.Plugins.CMS.Classes.Actions
 * @package   PsiFramework.Plugins.CMS.Classes.Actions
 * @author    Remi San <remi.san@gmail.com>
 * @copyright 2011 Remi San
 * @license   http://creativecommons.org/licenses/by/3.0/  Creative Commons CC BY 3.0
 * @version   SVN: <svn_id>
 * @link      http://www.psiframework.com/phpdoc/classes/db_\PsiFramework\Plugins.CMS.Classes.Actions.ListAction.html
 */

namespace PsiFramework\Plugins\CMS\Classes\Actions;

import("Classes.Actions.AbstractAction");
import("Classes.Factory.DAOFactory");
import("Classes.Util.Database.Filters.Paginator");
import("Classes.Util.Database.Query.SqlColumn");
import("Classes.Util.Database.Query.SqlOrder");
import("Classes.Util.Manager.ApplicationManager");

use \PsiFramework\Classes\Actions\AbstractAction;
use \PsiFramework\Classes\Factory\DAOFactory;
use \PsiFramework\Classes\Util\Database\Filters\Paginator;
use \PsiFramework\Classes\Util\Database\Query\SqlColumn;
use \PsiFramework\Classes\Util\Database\Query\SqlOrder;
use \PsiFramework\Classes\Util\Manager\ApplicationManager;
use \PsiFramework\Classes\Util\UI\TemplateManager;

/**
 * Default list action
 * 
 * This is the default class for the list action.
 *
 * @category PsiFramework.Plugins.CMS.Classes.Actions
 * @package  PsiFramework.Plugins.CMS.Classes.Actions
 * @author   Remi San <remi.san@gmail.com>
 * @license  http://creativecommons.org/licenses/by/3.0/  Creative Commons CC BY 3.0
 * @link     http://www.psiframework.com/phpdoc/classes/db_\PsiFramework\Plugins.CMS.Classes.Actions.ListAction.html
 */
class ListAction extends AbstractAction
{

    /**
     * The main function of the method which is called every time
     * an action is created by the front controller.
     * 
     * @see    AbstractAction::execute()
     * @return void
     */
    public function execute()
    {
        parent::execute();

        $dao = DAOFactory::getDAO($this->_objectType);
        if (is_null($dao)) {
        	throw new \Exception(
    			"Impossible to retrieve DAO for " . $this->_objectType
        	);
        }
        
        $idType = $this->_request->getParameter('id_type');

        $moreParams = array();
        $type = null ;
        if ($idType != null) {
            $moreParams["id_type"] = $idType ;
            $typeDao = DAOFactory::getDAO("Type");
            $type = $typeDao->retrieveByPk($idType);
        }
        //TODO gérer les params supplémentaires

        $sens = $this->_request->getParameter('sens');
        if ($sens == null) {
            $sens = "ASC" ;
        }
        $orderColumn = $this->_request->getParameter('orderColumn');
        $order = null ;

        $applicationManager = ApplicationManager::getInstance();
        $application = $applicationManager->getApplication();
        $objectParameters
            = $application->getObjectParameter($this->_objectType);

        if (isNullOrEmptyString($orderColumn)) {
            $orderColumn = 'id' ;
            $pk = $dao->getObjectParameters()->getPrimaryKey();
            if (!is_array($pk)) {
            	$orderColumn = $pk->name;
            } else if (count($pk)==1) {
            	$orderColumn = $pk[0]->name;
            }
        }
        $column = $objectParameters->getParameter($orderColumn);

        if (!isNullOrEmptyString($column)) {
            $sqlColumn = new SqlColumn($column->column);
            $order = new SqlOrder($sqlColumn, $sens);
        }

        $page = $this->_request->getParameter('page');
        $nbResults = $this->_request->getParameter('nbResults');

        $paginator = null ;
        if (!is_null($order)) {
            $paginator = new Paginator();
            $paginator->addOrder($order);

            if (isNullOrEmptyString($page)) {
                $page = 1 ;
            }
            if (isNullOrEmptyString($nbResults)) {
                $nbResults = 25 ;
            }

            $paginator->setNbResults($nbResults);
            $paginator->setPage($page);
            $totalResults = $dao->count();
        }

        $list = $dao->retrieveAll(null, $paginator);

        $this->_response->setAttribute("typeId", $idType);
        $this->_response->setAttribute("list", $list);
        $this->_response->setAttribute(
            "columns",
            $objectParameters->getParameters()
        );
        $this->_response->setAttribute("sens", $sens);
        $this->_response->setAttribute("orderColumn", $orderColumn);
        $this->_response->setAttribute("page", $page);
        $this->_response->setAttribute("nbResults", $nbResults);
        $this->_response->setAttribute("totalResults", $totalResults);
        $this->_response->setAttribute("moreParams", $moreParams);
        
        $this->_response->setAttribute("searchService", $dao->getSearchService());

        $templateManager = TemplateManager::getInstance();
        $templateManager->addRemoteScript(
        		'https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'
        );
        $templateManager->addRemoteScript(
        		'https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js'
        );
        
        $this->findForward('success');
    }

}
